/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.study.provider;


import com.study.api.DemoService;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ProtocolConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.config.ServiceConfig;

import java.util.stream.IntStream;

/**
 <dubbo:application name="demo-provider"/>
 <dubbo:registry address="multicast://224.5.6.7:1234"/>
 <dubbo:protocol name="dubbo" port="20880"/>
 <bean id="demoService" class="com.alibaba.dubbo.demo.provider.DemoServiceImpl"/>
 <dubbo:services interface="com.alibaba.dubbo.demo.DemoService" ref="demoService"/>
 */
public class Provider {

    public static void main(String[] args) throws Exception {
        // 当前应用配置
        ApplicationConfig application = new ApplicationConfig("demo-provider");
        // 连接注册中心配置
        RegistryConfig registry = new RegistryConfig("N/A");
        // 服务提供者协议配置
        ProtocolConfig protocol = new ProtocolConfig("dubbo",20880);
        DemoService demoService=new DemoServiceImpl();
        // 服务提供者暴露服务配置，注意：ServiceConfig为重对象，内部封装了与注册中心的连接，以及开启服务端口
        ServiceConfig<DemoService> service = new ServiceConfig<DemoService>(); // 此实例很重，封装了与注册中心的连接，请自行缓存，否则可能造成内存和连接泄漏
        service.setApplication(application);
        service.setRegistry(registry); // 多个注册中心可以用setRegistries()
        service.setProtocol(protocol); // 多个协议可以用setProtocols()
        service.setInterface(DemoService.class);
        service.setRef(demoService);
        service.setVersion("1.0.0");
        // 暴露及注册服务
        service.export();
        System.in.read();
    }

}
